package com.splic.api.controller.product;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.github.pagehelper.PageInfo;
import com.splic.common.core.controller.BaseController;
import com.splic.common.core.domain.ResponseObj;
import com.splic.common.utils.security.Md5Utils;
import com.splic.product.domain.Product;
import com.splic.product.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/product")
@Slf4j
public class ApiProductController extends BaseController {

    @Autowired
    private ProductService productServiceImpl;
    /**
     * 查询应用列表
     *
     * @return
     */
    @GetMapping("/getProductList")
    public ResponseObj<PageInfo<Product>> getProductList() {
        try{
            startPage();
            List<Product> list = productServiceImpl.selectPageList(null);
            return ResponseObj.SUCCESS(new PageInfo(list));
        }catch (Exception e){
            log.error("查询应用列表失败:{}",e);
            return ResponseObj.ERROR("查询应用列表失败。");
        }
    }

    /**
     * 查询推荐应用列表
     *
     * @return
     */
    @GetMapping("/getRecommendList")
    public ResponseObj<List<Map<String, String>>> getRecommendList(@RequestParam(value = "channelId", defaultValue = "") String channel,
                                                           @RequestParam(value = "phoneBrand", defaultValue = "") String phoneBrand) {
        try{
            List<Map<String, String>> list = productServiceImpl.selectRecommendPageList(channel, phoneBrand);
            return ResponseObj.SUCCESS(list);
        }catch (Exception e){
            log.error("查询推荐应用列表:{}",e);
            return ResponseObj.ERROR("查询推荐应用列表。");
        }
    }

    /**
     * 检查更新
     *
     * @return
     */
    @GetMapping("/isUpdateApp")
    public ResponseObj<Map<String, Object>> isUpdateApp(@RequestParam(value = "phoneBrand", defaultValue = "") String phoneBrand) {
        try{
            return ResponseObj.SUCCESS(productServiceImpl.isUpdateApp(phoneBrand));
        }catch (Exception e){
            log.error("检查更新:{}",e);
            return ResponseObj.ERROR("检查更新。");
        }
    }

    /**
     * 获取oaid证书
     *
     * @return
     */
    @GetMapping("/getOaidFile")
    public ResponseObj<Map<String, Object>> getOaidFile() {
        try{
            return ResponseObj.SUCCESS(productServiceImpl.getOaidFile());
        }catch (Exception e){
            log.error("获取oaid证书:{}",e);
            return ResponseObj.ERROR("获取oaid证书。");
        }
    }

    public static void main(String[] args) {
        String time = String.valueOf(System.currentTimeMillis());
        String oak = "d1a15aa459824018";
        String secret = "5b351d49d371222c1f14dc3dc725205f";
        String path = "/resopen/app/version/ej.easyjoy.alarmandreminder.cn";
        String param = oak+secret+time+path;
        String sign= Md5Utils.hash(param + param.length());
        HttpResponse res = HttpRequest.get("https://appstore-openapi-cn.heytapmobi.com/resopen/app/version/ej.easyjoy.alarmandreminder.cn").header("oak", oak).header("sign", sign).header("t", time).execute();
        System.out.println(res);
    }
}
